/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import database.DBNames;

/**
 *
 * @author falxa
 */
public class Node 
{
    private int id;
    private String type;
    private double weight;
    private String xpath;
    private int fdoc;
    
    private ArrayList<Term> terms;
    
    
    public Node(int id, String type, float weight, String xpath, int fdoc) {
		super();
		this.id = id;
		this.type = type;
		this.weight = weight;
		this.xpath = xpath;
		this.fdoc = fdoc;
		this.terms = new ArrayList<>();
	}

	public Node(int id, int fdoc, String type, String xpath)
    {
        this.id = id;
        this.fdoc = fdoc;
        this.type = type;
        this.xpath = xpath;
        this.terms = new ArrayList<>();
    }
    
    public Node(int id, String type, float weight, String xpath) 
    {
        this.id = id;
        this.type = type;
        this.weight = weight;
        this.xpath = xpath;
        this.terms = new ArrayList<>();
    }
    
    public Node(ResultSet rs)
    {
    	try
		{
	    	this.id = rs.getInt(DBNames.NODE_ID);
	        this.type = rs.getString(DBNames.NODE_TYPE);
	        this.weight = rs.getFloat(DBNames.NODE_WEIGHT);
	        this.xpath = rs.getString(DBNames.NODE_XPATH);
	        this.fdoc = rs.getInt(DBNames.NODE_FDOC);
	        this.terms = new ArrayList<>();
		} catch (SQLException e)
		{
			e.printStackTrace();
		}
    }
    
    public StringBuilder toDBString()
    {
        StringBuilder toDB = new StringBuilder("(\""+this.id+"\", \""+this.type+"\", \""+this.weight+"\", \""+this.xpath+"\", \""+this.fdoc+"\"),");
        return toDB;
    }
    
    public int countTerm(int term)
    {
    	int q = 0;
    	for (Term t: terms)
    		if (t.getID() == term)
    			q++;
    	return q;
    }

    public int getFdoc() 
    {
        return fdoc;
    }
    
    public int getID()
    {
    	return this.id;
    }
    
    public String getType()
    {
    	return this.type;
    }
    
    public boolean isP()
    {
    	return (this.type.compareTo("P")==0);
    }
    
    public String getXPath()
    {
    	return this.xpath;
    }
    
    public ArrayList<Term> getTerms()
    {
    	return this.terms;
    }
    
    public void addTerm(Term t)
    {
    	this.terms.add(t);
    }
    
    public double getWeight()
    {
    	return this.weight;
    }
    
    public void setWeight(double w)
    {
    	this.weight = w;
    }
    
    
}
